package com.techwells.learningkites.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.os.Bundle;
import android.widget.ListView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseExpandListAdapter;
import com.techwells.learningkites.adapter.MyWordListAdapter;
import com.techwells.learningkites.adapter.MyWordListAdapter;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.model.MyWordViewModel;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.FavoriteWord;
import com.techwells.learningkites.networkservice.model.Word;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.networkservice.ServiceMediator;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.ToastUtils;
import com.techwells.taco.widget.DelSlideListView;
import com.techwells.taco.widget.OnDeleteListioner;

public class MyWordActivity extends CommonBaseActivity implements OnDeleteListioner {
	
	private DelSlideListView lvMywords;
	private List<Word> list;
	private MyWordListAdapter adapter;
	
	private MyWordViewModel presentViewModel;
	private List<FavoriteWord> wordList;
	private int deletePosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mywords);
		
//		initTitleBar(R.string.my_word, defaultLeftClickListener);
		initViews();
	}

	private void initViews() {
		lvMywords = (DelSlideListView) findViewById(R.id.lv_mywords);
//		list = new ArrayList<Word>();
//		for (int i = 0; i < 6; i++) {
//			Word word = new Word();
//			list.add(word);
//		}
//		adapter = new MyWordListAdapter(this, list);
//		lvMywords.setAdapter(adapter);
//		lvMywords.setDeleteListioner(this);
//		adapter.setOnDeleteListioner(this);
	}

	@Override
	public boolean isCandelete(int position) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onDelete(int ID) {
		if (wordList.size() > 0) {
			deletePosition = ID;
			doRequestDeleteWord(wordList.get(ID).uwid);
		}
//		list.remove(ID);
//		lvMywords.deleteItem();
//		adapter.notifyDataSetChanged();
		
	}


	@Override
	public void onBack() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		
		presentViewModel = (MyWordViewModel) baseViewModel;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		doRequestGetWords();
	}


	private void doRequestGetWords() {
		doTask(KitesServiceMediator.SERVICE_GET_ALL_WORDS,null);
	}
	
	private void doRequestDeleteWord(int uwid) {
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("uwid", uwid);
		doTask(KitesServiceMediator.SERVICE_DELETE_WORDS,argMap);
	}
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_ALL_WORDS)) {
			wordList = ((MyWordViewModel)baseViewModel).wordList;
			adapter = new MyWordListAdapter(this, wordList);
			lvMywords.setAdapter(adapter);
			lvMywords.setDeleteListioner(this);
			adapter.setOnDeleteListioner(this);
			
			initTitleBar(getResources().getString(R.string.my_word) + "( "+wordList.size() + " )", defaultLeftClickListener);
		}else if (method.equals(KitesServiceMediator.SERVICE_DELETE_WORDS)) {
			int returnCode = ((MyWordViewModel)baseViewModel).returnCode;
			if (returnCode == ServiceMediator.Service_Return_Success) {
				wordList.remove(deletePosition);
				lvMywords.deleteItem();
				adapter.notifyDataSetChanged();
				ToastUtils.show(this, "删除成功");
			}
		}
	}
	
}
